<?php
class controller_admin_index extends system_controller_action {
	
	private static $_namespace = 'HIHICARADMIN';
	
	private $user = null;
	public function init() {
		$this->layout()->setLayout('panelLayout');
		$this->view->language = $this->view->language();
		$this->view->user = $this->user = system_user::getInstance()->getRow();
	}

	public function action_system() {
		if($this->adminLogin()) {
			return ;
		}
		$this->render();
	}
	
	public function action_supplier() {
		if($this->adminLogin()) {
			return ;
		}
		$this->render();
	}
	
	public function action_catalog() {
		if($this->adminLogin()) {
			return ;
		}
		$this->render();
	}
	
	public function action_index() {
		if($this->adminLogin()) {
			return ;
		}
		/**
		 * 选择语言
		 */
		if($this->getRequest()->isPost() && ($language = $this->getRequest()->getPost('language'))) {
			system_passport::getInstance()->getStorage()->setLanguage($language)->save();
			if(!!($referer = $this->getRequest()->getHeader('referer'))) {
				$this->getResponse()->setRedirect($referer);
			} else {
				$this->redirector()->gotoRouteAndExit('index/index');
			}
		}
		$this->render();
	}
		
	public function action_login() {
		$this->view->request = $this->getRequest();
		$this->view->lang = system_passport::getInstance()->getStorage()->getLanguage();	
		$this->view->error = array();
		if($this->getRequest()->isPost()) {
			$referer = trim($this->getRequest()->getPost('referer'));
			$lang = trim($this->getRequest()->getPost('language'));
			try {
				if($this->getRequest()->getPost('changeLanguage')) {
					system_passport::getInstance()->getStorage()->setLanguage($lang)->save();
					$this->view->lang = $lang;
					$this->view->language()->reload();
				} else {
					if(!system_captcha::auth($this->getRequest()->getPost('validCode'), self::$_namespace)) {
						$this->view->error['validCode'] = 'Valid code is not matched.';
					}
					
					$account = trim($this->getRequest()->getPost('account'));
					$password = trim($this->getRequest()->getPost('password'));
					$concept = new system_user_concept();
					if(!($row = $concept->fetch(array('identity=?' => $account)))) {
						$this->view->error['auth'] = 'Authenticate failed.';
					}
					if($row && !$row->auth($password)) {
						$this->view->error['auth'] = 'Authenticate failed.';
					}
					
					if(count($this->view->error)==0) {					
						$secure = system_passport::getInstance()
								->setStorage(($this->getRequest()->getPost('remember') ? 'cookie' : 'session'))
								->getStorage()
								->setId($row->getId())
								->setIdentity($account)
								->setSecurity(intval($this->getRequest()->getPost('secure')))
								->setLanguage($lang)
								->save();
						
						$row->setSecure($secure->getSecurity());
						$row->setLastupip($this->getRequest()->getClientIp());
						$row->setLastupdate(system_date::timestamp());
						$row->update();		
						
						if($referer) {
							$this->getResponse()->setRedirect($referer);
						} else {
							$this->redirector()->gotoRouteAndExit('index/index');
						}
					}
				}
				$this->view->referer = $referer;
			} catch (Exception $e) {
				$this->view->exception = $e;
			}			
		}
		$this->layout()->disableLayout();
		$this->render();
	}
	
	public function action_logout() {
		system_passport::getInstance()->getStorage()->clean();
		$referer = $this->getRequest()->getHeader('referer');
		if($referer) {
			$this->getResponse()->setRedirect($referer);
		} else {
			$this->redirector()->gotoRouteAndExit('index/index');
		}
	}
	
	public function action_error() {
		if(!$this->view->exception) {
			$this->view->exception = $this->getResponse()->getException();
		}
		$this->layout()->setLayout('layout');
		$this->render();
	}
	
	public function action_captcha() {
		system_captcha::getInstance(self::$_namespace)->setCode()->display();
	}
	
	public function action_robots() {
		$this->layout()->disableLayout();
		$this->render('robots.txt');
	}
}